package com.eyet.comeon.main;

import java.util.List;

import com.eyet.comeon.bean.Game;
import com.eyet.comeon.R;
import com.eyet.comeon.data.ActionIntent;
import com.eyet.comeon.data.Data;
import com.eyet.comeon.protocol.Protocol;
import com.eyet.comeon.ui.UI;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {
	
	private static final int DIALOG_ADD = 1;
	
	private List<Game> listItem;
	private ListView list;
	private ImageView iv_add;
	private Handler handler;
	private ProgressDialog pd;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //控件初始化
    	initViews();
    }
	
	private void initViews(){
		handler = new Handler();
		list = (ListView) this.findViewById(R.id.list);
    	iv_add = (ImageView)this.findViewById(R.id.add);
    	pd = UI.createProgressDialog(this, "提示", "正在努力的加载...");
    	pd.show();
    	//监听注册
    	initListener();
	}
	
	private void initListener(){
		list.setOnItemClickListener(new OnItemClickListener(){

			public void onItemClick(AdapterView<?> parent, View view, 
					int position, long id) {
				Data.gid = Integer.valueOf(listItem.get(position).gid);
				Data.gname = listItem.get(position).gname;
				Intent intent = new Intent(ActionIntent.ACTION_SHAKE);
				MainActivity.this.startActivity(intent);
			}
    		
    	});
		
		iv_add.setOnClickListener(new OnClickListener(){

			public void onClick(View arg0) {
				showDialog(DIALOG_ADD);
			}
			
		});
		
    	//调用协议函数
		doGame_list();
	}
	
	private void doGame_list(){
		
		new Thread(new Runnable(){
			public void run(){
				Protocol pro = new Protocol();
				boolean status = pro.Game_listProtocol();
				if(status){
					listItem = Data.game_list;
					handler.post(new Runnable(){
						public void run(){
							initList();
						}
					});
				}else{
					handler.post(new Runnable(){
						public void run(){
							UI.createDialog(MainActivity.this, "抱歉，网络获取失败，请再尝试一次。", 1);
						}
					});
				}
				pd.dismiss();
			}
		}).start();
		
	}
	
	private void initList(){
		list.setAdapter(new DataListAdapter(this));
	}
	
	/*
     * 列表适配器
     */
    private class DataListAdapter extends BaseAdapter{
    	
    	private LayoutInflater mInflater;
		
		public DataListAdapter(Context context){
			mInflater = LayoutInflater.from(context);
		}
		
		public int getCount() {
			return listItem.size();
		}

		public Object getItem(int position) {
			return listItem.get(position);
		}

		public long getItemId(int position) {
			long id = Integer.valueOf(listItem.get(position).gid);
			return id;
		}

		public View getView (final int position, View convertView, ViewGroup parent) {
			View viewItem;
			TextView tv_gname;
			TextView tv_bz;
			
			viewItem = mInflater.inflate(R.layout.list_item_1, null);
			tv_gname = (TextView) viewItem.findViewById(R.id.gname);
			tv_bz = (TextView) viewItem.findViewById(R.id.bz);
			
			Game game = listItem.get(position);
			
			tv_gname.setText(game.gname);
			tv_bz.setText(game.bz);
			
			return viewItem;
		}
    	
    }
    
    
    @Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog=null;
		switch(id){
		case DIALOG_ADD:
			
			LayoutInflater li = LayoutInflater.from(this);  
		    View view = li.inflate(R.layout.prompt_view, null);  
		    
		    AlertDialog.Builder builder = new AlertDialog.Builder(this);  
		    builder.setTitle("分享比赛,让世界一同脉动.");  
		    builder.setIcon(android.R.drawable.ic_menu_more);  
		    //之前inflate的View 放到dialog中  
		    builder.setView(view);  
		    builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
				public void onClick(DialogInterface dialog, int which) {
					AlertDialog ad = (AlertDialog) dialog;  
			        final EditText et_name = (EditText) ad.findViewById(R.id.promp_name);
			        final EditText et_bz = (EditText) ad.findViewById(R.id.promp_bz);
			        final String gname = et_name.getText().toString();
			        final String bz = et_bz.getText().toString();
			        
			        new Thread(new Runnable(){
			        	public void run(){
			        		Protocol pro = new Protocol();
			        		boolean status = pro.Game_addProtocol(gname , bz);
			        		if(status){
			        			Intent intent = new Intent(ActionIntent.ACTION_MAIN);
			        			startActivity(intent);
			        			finish();
			        		}
			        	}
			        }).start();
			        
			        
				}
		    });  
		    builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int which) {
                	
                }
		    });  
		    dialog = builder.create();  
			break;
		}
		return dialog;
	}

}
